﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Net;
using Redboil.Bfp.Core;
using Redboil.Bfp.Model;

namespace Redboil.Bfp.Client {
    public partial class SampleView : Window {
        public SampleView() {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e) {
            var window = new SampleWindow();
            window.Show();
        }

        private void button2_Click(object sender, RoutedEventArgs e) {
            var window = new MainWindow();
            window.Show();
        }

        private void button3_Click(object sender, RoutedEventArgs e) {

            var webClient = new WebClient();
            webClient.DownloadStringCompleted += (o, ea) => {
                var jsonSerializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(IList<User>));
                using(var memoryStream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(ea.Result))) {
                    MessageBox.Show(ea.Result);
                    var userList = jsonSerializer.ReadObject(memoryStream) as IList<User>;
                    MessageBox.Show(userList.Count.ToString());
                }
            };
            webClient.DownloadStringAsync(new Uri("http://localhost:1000/SampleService"));


            //var client = new WebClient();
            //client.DownloadStringCompleted += (o, ea) => {
            //    MessageBox.Show(ea.Result);
            //    //var userList = JsonHelper.Deserialize<User>(ea.Result);

            //    var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(IList<User>));
            //    var bytesResult = Encoding.UTF8.GetBytes(ea.Result);
            //    using(var stream = new System.IO.MemoryStream(bytesResult)) {
            //        var userList = serializer.ReadObject(stream) as IList<User>;
            //        MessageBox.Show(userList.Count.ToString());
            //    }

                
            //};
            //client.DownloadStringAsync(new Uri("http://localhost:1000/SampleService"));
        }

        private void button4_Click(object sender, RoutedEventArgs e) {
            var client = new WebClient();
            client.Headers[HttpRequestHeader.ContentType] = "text/json";
            client.Encoding = Encoding.UTF8;

            IList<User> userList = new List<User>() {
                new User() { Account = "Gene", IsAdministrator = true, Password = "123456" }
            };
            var json = JsonHelper.Serialize<User>(userList);

            client.UploadStringCompleted += (o, ea) => {
                var x = ea.Result;
            };
            client.UploadStringAsync(new Uri("http://localhost:1000/SampleService"), "POST", json);
        }

        private void button5_Click(object sender, RoutedEventArgs e) {
            //var view = new HomeView();
        }
    }
}
